package com.glela.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.glela.platform.constant.ErrorEnum;
import com.glela.platform.model.User;
import com.glela.platform.model.UserInfo;
import com.glela.platform.model.response.ResponseBaseModel;
import com.glela.platform.model.response.ResponseResultModel;
import com.glela.platform.service.UserService;
import com.glela.platform.util.ParameterUtils;
import com.glela.serial.model.request.RequestSerial;
import com.glela.serial.model.response.ResponseSerialDetail;
import com.glela.serial.service.JiangshiSerialService;

/**
 * 校妆APP讲师专区讲师店铺接口逻辑控制器
 * 
 * @author 卫耀华
 * @date 2016-12-07
 */
@Controller
@RequestMapping(value = "/jiangshi", produces = "application/json;charset=UTF-8")
public class JiangshiSerialController extends BaseController {
	@Autowired
	private JiangshiSerialService	jiangshiSerialService;
	@Autowired
	private UserService				userService;

	/**
	 * 首页讲师专区店铺入口(校妆APP)
	 * 
	 * @author 卫耀华
	 * @date 2016-12-07
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/homePageJiangShiStore", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public ResponseEntity<?> homePageJiangShiStore(@RequestBody RequestSerial request) {
		String rslt = null;
		try {
			// 验证登录用户的ID
			if (ParameterUtils.validParams(request.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			rslt = queryCacheResult(request.getUserId());
			if (StringUtils.isNotEmpty(rslt)) {
				return toRespEntity(rslt);
			}
			// 获取当前登录的用户信息
			User user = userService.getUser(request.getUserId());
			if (null == user) {
				rslt = toErrorModelJSON(ErrorEnum.C20113);
			} else if (StringUtils.isEmpty(user.getRecommendFrom())) {
				// 此用户没有普通推荐人或者普通推荐人不存在，无需展示讲师店铺专区
				rslt = toErrorModelJSON(ErrorEnum.C29912);
			} else {
				//查询推荐人身份信息
				UserInfo recommendUserInfo = userService.selectUserInfoByPersionId(user.getRecommendFrom());
				if (recommendUserInfo == null) {
					rslt = toErrorModelJSON(ErrorEnum.C29912);
				} else {
					// 获取用户的推荐人信息
					User recommendUser = userService.getUser(recommendUserInfo.getUserId());
					if (null == recommendUser) {
						rslt = toErrorModelJSON(ErrorEnum.C20113);
					} else {
						List<Integer> roleIdList = new ArrayList<Integer>();
						roleIdList.add(3);
						roleIdList.add(4);
						roleIdList.add(5);
						roleIdList.add(6);
						roleIdList.add(7);
						roleIdList.add(11);
						/* 如果该用户的推荐人的角色不是34567(学生店主s1 学生店主s2 学生店主s3 学生店主s4 讲师)，就不展示店铺 */
						if (!roleIdList.contains(recommendUser.getRoleId())) {
							rslt = toErrorModelJSON(ErrorEnum.C29912);
						} else {
							Map<String, Object> map = new HashMap<String, Object>();
							map.put("talentId", recommendUser.getId());// 讲师ID
							map.put("talentHeadPhoto", StringUtils.isEmpty(recommendUser.getPicture()) ? "" : recommendUser.getPicture());// 达人头像
							map.put("talentNickName", StringUtils.isEmpty(recommendUser.getNickName()) ? "" : recommendUser.getNickName());// 达人昵称
							map.put("backgroundImage", recommendUser.getBackgroundImage());// 达人主页背景图
							rslt = toResultModelJSON(map);
						}
					}
				}
			}
		} catch (Exception e) {
			getLOGGER().error("APP首页是否展示店铺接口异常", e);
			rslt = toErrorModelJSON(ErrorEnum.C20000);
		}
		return toRespEntity(toCacheResult(request.getUserId(), rslt));
	}

	/**
	 * 讲师信息和下面的期资讯信息(校妆网APP)
	 * 
	 * @author 卫耀华
	 * @date 2016-12-08
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/jiangShiStoreSerial", method = RequestMethod.POST, consumes = "application/json")
	@ResponseBody
	public ResponseEntity<?> jiangShiStoreSerial(@RequestBody RequestSerial request) {
		/* 验证达人ID */
		try {
			if (ParameterUtils.validParams(request.getTalentId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			return jiangshiSerialService.getTeacherSerial(request.getUserId(), request.getTalentId());
		} catch (Exception e) {
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}

	/**
	 * 讲师店铺的期详情(校妆网APP) B店期详情
	 * 
	 * @author 卫耀华
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/jiangshiSrialDetail", method = RequestMethod.POST, consumes = "application/json")
	public ResponseEntity<?> jiangshiSrialDetail(@RequestBody RequestSerial request) {
		try {
			// 验证期资讯ID 用户ID 分享者ID 并赋默认值
			if (ParameterUtils.validParams(request.getSerialId(), request.getUserId())) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20102), HttpStatus.OK);
			}
			User user = userService.getUser(request.getUserId());
			if (null == user) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20113), HttpStatus.OK);
			}
			if (request.getShareUserId() == null) {
				request.setShareUserId(0L);
			}
			//获取期资讯信息，包含期资讯下的商品和处理之后的价格
			ResponseSerialDetail detail = jiangshiSerialService.getDetailBySerialId(user.getRoleId(), request.getShareUserId(), request.getSerialId());
			if (detail == null) {
				return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20137), HttpStatus.OK);
			}
			return new ResponseEntity<>(new ResponseResultModel<ResponseSerialDetail>(detail), HttpStatus.OK);
		} catch (Exception e) {
			getLOGGER().error("B店期详情异常", e);
			return new ResponseEntity<>(new ResponseBaseModel(ErrorEnum.C20000), HttpStatus.OK);
		}
	}
}
